import {Component, OnInit} from '@angular/core';
import {HttpParams} from '@angular/common/http';
import {NzMessageService, NzModalRef, NzModalService} from 'ng-zorro-antd';
import {Router} from '@angular/router';
import {AppService} from '../../service/app/app.service';
import {DataServerService} from '../../service/data-server';

@Component({
  selector: 'app-buy-time',
  templateUrl: './buy-time.component.html',
  styleUrls: ['./buy-time.component.css']
})
export class BuyTimeComponent implements OnInit {
  isLoading = true;
  commoditys = [];
  general = [];
  advance = [];
  steady = [];
  currentCommdity = [];
  nzTip = '正在加载商品...';
  styleInfo = '普通版VIP';
  styleValue = 1;
  isSpinning = true;
  money: any;
  chargeModal: NzModalRef;
  constructor(private appService: AppService,
              private message: NzMessageService,
              private dataServce: DataServerService,
              private modalService: NzModalService,
              private router: Router) { }

  ngOnInit() {
    this.getMoneyInfo();
    this.appService.get(this.appService.urls.shop, {observe: 'response', params: new HttpParams().append('access_token', localStorage.getItem('token'))}).subscribe((resp: any) => {
      if (resp.body.ret === 1) {
        this.commoditys = resp.body.data;
        for (const item of this.commoditys) {
          if (item.name.indexOf('普通版') === 0) {
            this.general.push(item);
          }
          if (item.name.indexOf('高级版') === 0) {
            this.advance.push(item);
          }
          if (item.name.indexOf('加强版') === 0) {
            this.steady.push(item);
          }
        }
        this.currentCommdity = this.general;
        this.isSpinning = false;
      }
    });
  }

  getMoneyInfo() {
    this.appService.get(this.appService.urls.userinfo + localStorage.getItem('user_id'), {observe: 'response', params: new HttpParams().append('access_token', localStorage.getItem('token'))}).subscribe((resp: any) => {
      if (resp.body.ret === 1) {
        this.money = resp.body.data.money;
      }
    });
  }
  styleChange(kind) {
      switch (kind) {
        case 1:
          this.styleValue = 1;
          this.currentCommdity = this.general;
          this.styleInfo = '普通版VIP';
          break;
        case 2:
          this.styleValue = 2;
          this.currentCommdity = this.advance;
          this.styleInfo = '高级版SVIP';
          break;
        case 3:
          this.styleValue = 3;
          this.currentCommdity = this.steady;
          this.styleInfo = '加强版SSVIP';
          break;
      }
  }
  backToHome() {
    this.router.navigate(['home']).catch();
  }
  buyTime(item: number) {
    this.isSpinning = true;
    if (this.commoditys.length === 0) {
      this.message.info('正在准备商品');
      return;
    }
    this.isSpinning = true;
    let currentCommodity;
    switch (this.styleValue) {
      case 1:
        currentCommodity = this.general[item];
        break;
      case 2:
        currentCommodity = this.advance[item];
        break;
      case 3:
        currentCommodity = this.steady[item];
        break;
    }
      if (currentCommodity !== undefined && currentCommodity !== null && parseFloat(currentCommodity.price) < parseFloat(this.money)) {
      this.buyShop(currentCommodity.id);
    } else {
      this.chargeModal = this.modalService.confirm({
        nzTitle: '提示',
          nzContent: '总价' + currentCommodity.price + ',余额' + this.money + ', 是否充值?',
        nzOnOk: () => {
          this.isSpinning = false;
          this.chargeModal.destroy();
          this.dataServce.data = {
              money: Math.ceil(parseFloat(currentCommodity.price) - parseFloat(this.money)),
            router: 'buy'
          };
          this.router.navigate(['charge']).catch();
        },
        nzOnCancel: () => {
          this.chargeModal.destroy();
          this.isSpinning = false;
          return;
        }
      });
    }
  }
  buyShop(shopId) {
    this.appService.post(this.appService.urls.buy +  localStorage.getItem('user_id') + '?access_token=' + localStorage.getItem('token'), {shopid: shopId}).subscribe((resp: any) => {
      const result = resp.body;
      this.isSpinning = false;
      if (result.ret === 1) {
        this.modalService.success({
          nzTitle: '购买结果',
          nzContent: result.msg
        });
      } else {
        this.modalService.warning({
          nzTitle: '购买结果',
          nzContent: result.msg
        });
      }
    });
  }
}
